import java.util.Scanner;
import java.io.*;


public class DragonChessRunner {
	public static void main(String[] args) throws FileNotFoundException {
		PlayingField pField = setUpBoard();
		
		printBoard(pField);
		
		for(int i = 0; i < 10; i++){
			playMove(pField);
		}
	}
	
	public static void playMove(PlayingField pField) {
		Scanner console = new Scanner(System.in);
		System.out.println("Where is your piece? (board column row)");
		int board = console.nextInt();
		int col = console.nextInt();
		int row = console.nextInt();
		Piece move = pField.getPiece(board, col, row);
		System.out.println("Where would you like to move to? (board column row)");
		int boardTo = console.nextInt();
		int colTo = console.nextInt();
		int rowTo = console.nextInt();
		Square dest = pField.getSquare(boardTo, colTo, rowTo);
		if (!move.moveTo(dest)){
			System.out.println("That piece can't move there!");
		}
		printBoard(pField);
	}
	
	//________________________________________________
	public static PlayingField setUpBoard() throws FileNotFoundException {
		PlayingField pField = new PlayingField();
		
		//adds Boards to pField and initializes all Squares
		for(int l = 0; l < 3; l++) {
			pField.addBoard(l);
			for(int c = 0; c < 12; c++) {
				for(int r = 0; r < 8; r++) {
					pField.addSquare(l, c, r);
				}
			}
		}

		scanLayout(pField, new File("src/StartLayout.txt"));
		
		return pField;
	}

	//scans the file that shows the starting layout and gives the Squares Pieces accordingly
	private static void scanLayout(PlayingField pField, File f) throws FileNotFoundException {
		Scanner fileScan = new Scanner(f);
		for(int l = 2; l >= 0; l--) {	//startLayout.txt is arranged with the skyBoard first
			for(int r = 0; r < 8; r++) {
				fileScan.nextInt();
				for(int c = 0; c < 12; c++) {
					char next = fileScan.next().toCharArray()[0];
					int nextSide = 0;
					if(isUppercase(next)) {
						nextSide = 1;
					} else if(isLowercase(next)) {
						nextSide = 2;
					}
					if(nextSide != 0) {
						translate(next, pField, l, c, r, nextSide);
					}
				}
			}
		}
	}
	
	//translates a char to a Piece and adds it to the pField
	private static void translate(char p, PlayingField pField, int l, int c, int r, int side) {
		if(p == 'w' || p == 'W') {
			pField.addPiece(l, c, r, new Warrior(side));
		} else if(p == 'r' || p == 'S') {
			pField.addPiece(l, c, r, new Sylph(side));							
		} else if(p == 'd' || p == 'D') {
			pField.addPiece(l, c, r, new Dwarf(side));
		} else if(p == 'g' || p == 'G') {
			pField.addPiece(l, c, r, new Griffin(side));
		} else if(p == 'c' || p == 'R') {
			pField.addPiece(l, c, r, new Dragon(side));
		} else if(p == 'o' || p == 'O') {
			pField.addPiece(l, c, r, new Oliphant(side));
		} else if(p == 'u' || p == 'U') {
			pField.addPiece(l, c, r, new Unicorn(side));
		} else if(p == 'h' || p == 'H') {
			pField.addPiece(l, c, r, new Hero(side));
		} else if(p == 't' || p == 'T') {
			pField.addPiece(l, c, r, new Thief(side));
		} else if(p == 'c' || p == 'C') {
			pField.addPiece(l, c, r, new Cleric(side));
		} else if(p == 'm' || p == 'M') {
			pField.addPiece(l, c, r, new Mage(side));
		} else if(p == 'k' || p == 'K') {
			pField.addPiece(l, c, r, new King(side));
		} else if(p == 'p' || p == 'P') {
			pField.addPiece(l, c, r, new Paladin(side));
		} else if(p == 'b' || p == 'B') {
			pField.addPiece(l, c, r, new Basilisk(side));
		} else if(p == 'e' || p == 'E') {
			pField.addPiece(l, c, r, new Elemental(side));
		}
	}
	
	//prints the board as the pieces that occupy the Squares (skyBoard first)
	private static void printBoard(PlayingField b) {
		System.out.println();
		for(int l = 2; l >= 0; l--) {
			for(int r = 0; r < 8; r++) {
				if(r == 4) {
					System.out.print("(" + l + ")");
				}
				System.out.print("\t" + r + "   ");
				for(int c = 0; c < 12; c++) {
					if(b.getPiece(l, c, r) == null) {
						System.out.print("- ");
					} else {
						System.out.print(b.getPiece(l, c, r) + " ");
					}
				}
				System.out.println();
			}
			System.out.println();
			System.out.print("\t    0 1 2 3 4 5 6 7 8 9 1011 ");
			System.out.println("\n\n");
		}
	}
	
	private static boolean isUppercase(char c) {
		return (int)c >= 65 && (int)c <= 90;
	}
	
	private static boolean isLowercase(char c) {
		return (int)c >= 97 && (int)c <= 122;
	}

}
